package com.zhangjingcheng.yunyixia.controller;

import com.zhangjingcheng.yunyixia.pojo.dto.UpdatePwdDTO;
import com.zhangjingcheng.yunyixia.pojo.dto.UserLoginDTO;
import com.zhangjingcheng.yunyixia.pojo.dto.UserRegisterDTO;
import com.zhangjingcheng.yunyixia.pojo.vo.UserLoginVO;
import com.zhangjingcheng.yunyixia.pojo.vo.UserSpaceVO;
import com.zhangjingcheng.yunyixia.properties.JwtProperties;
import com.zhangjingcheng.yunyixia.result.Result;
import com.zhangjingcheng.yunyixia.service.UserService;
import io.swagger.annotations.ApiOperation;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 用户注册
     *
     * @param dto
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody UserRegisterDTO dto) {
        log.info("用户注册：{}", dto);
        userService.register(dto);
        return Result.success("注册成功");
    }

    /**
     * 用户登录
     *
     * @param dto
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO dto) {
        log.info("用户登录：{}", dto);
        return Result.success(userService.login(dto));
    }

    /**
     * 重置密码
     *
     * @param dto
     * @return
     */
    @ApiOperation("重置密码")
    @PostMapping("/resetPwd")
    public Result<String> resetPwd(@RequestBody UserLoginDTO dto) {
        log.info("重置密码：{}", dto);
        userService.resetPwd(dto);
        return Result.success("重置密码成功");
    }

    /**
     * 获取用户空间信息
     *
     * @return
     */
    @ApiOperation("获取用户空间信息")
    @GetMapping("/getUserSpace")
    public Result<UserSpaceVO> getUserSpace() {
        log.info("获取用户空间信息");
        return Result.success(userService.getUserSpace());
    }

    /**
     * 获取用户头像
     *
     * @return
     */
    @ApiOperation("获取用户头像")
    @GetMapping("/getUserAvatar")
    public Result<String> getUserAvatar() {
        return Result.success(userService.getUserAvatar());
    }

    /**
     * 更新用户头像
     *
     * @param avatarUrl
     * @return
     */
    @ApiOperation("更新用户头像")
    @PatchMapping("/updateAvatar")
    public Result<String> updateAvatar(@URL String avatarUrl) {
        log.info("更新用户头像：{}", avatarUrl);
        userService.updateAvatar(avatarUrl);
        return Result.success();
    }

    /**
     * 更新用户密码
     *
     * @param dto
     * @return
     */
    @ApiOperation("更新用户密码")
    @PatchMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody UpdatePwdDTO dto) {
        log.info("更新用户密码：{}", dto);
        userService.updatePwd(dto);
        return Result.success();
    }

    /**
     * QQ登录
     *
     * @param code
     * @return
     */
    @ApiOperation("QQ登录")
    @GetMapping("/qqLogin")
    public ModelAndView qqLogin(String code, HttpServletResponse response, HttpSession session) {
        log.info("QQ登录：{}", code);
        return userService.qqLogin(code, response, session);
    }

    /**
     * 退出登录
     *
     * @return
     */
    @ApiOperation("退出登录")
    @GetMapping("/logout")
    public Result<String> logout(HttpServletRequest request) {
        String token = request.getHeader(jwtProperties.getTokenName());
        log.info("退出登录");
        userService.logout(token);
        return Result.success();
    }

    /**
     * 获取用户信息
     *
     * @return
     */
    @ApiOperation("获取用户信息")
    @GetMapping("/getUserInfo")
    public Result<UserLoginVO> getUserInfo(HttpSession session) {
        log.info("获取用户信息");
        return Result.success(userService.getUserInfo(session));
    }

    /**
     * 用户重命名
     *
     * @param name
     * @return
     */
    @ApiOperation("用户重命名")
    @PatchMapping("/rename")
    public Result<String> rename(String name) {
        log.info("用户重命名：{}", name);
        userService.userRename(name);
        return Result.success();
    }

}
